/*
 * Light.cpp
 *
 *  Created on: 01/05/2013
 *      Author: gabriel
 */

#include "Light.hpp"
#include <iostream>

Light::Light() {

	for (int i = 0; i < 4; i++) {
		ambient[i] = 1.0f ;
		diffuse[i] = 1.0f;
		specular[i] = 1.0f;
		position[i] = 0.0f;
	}

}



Light::~Light() {
}

void Light::setMatrix(Matrix4f& mat) {
	this->matrix.copy(mat);
}

void Light::translate(float x, float y, float z){
	this->matrix.translate(x,y,z);
}

void Light::rotate(float angle, float x, float y, float z) {
	this->matrix.rotate(angle, x, y, z);
}


void Light::addShader(Shader *program){

	programs.push_back(*program);

	setup();


}

void Light::setup()
{
	for(unsigned int i =0; i<programs.size(); i++){

		Shader shader = programs.at(i);
		glUseProgram(shader.programHandle);

		int lightambientUniformHandle = shader.getUniformHandle("light_ambient");
		int lightdiffuseUniformHandle = shader.getUniformHandle("light_diffuse");
		int lightspecularUniformHandle = shader.getUniformHandle("light_specular");
		if (lightambientUniformHandle == -1 || lightdiffuseUniformHandle == -1 || lightspecularUniformHandle == -1){
			return;
		}

		glUniform4fv(lightambientUniformHandle, 1, ambient);
		glUniform4fv(lightdiffuseUniformHandle, 1, diffuse);
		glUniform4fv(lightspecularUniformHandle, 1, specular);
	}
}


void Light::setColour(float ambientColour[4], float diffuseColour[4],
		float specularColour[4]) {

	for(int i = 0; i< 4; i++){
		ambient[i]  = ambientColour[i] ;
		diffuse[i]  = diffuseColour[i];
		specular[i] = specularColour[i];
	}

	setup();

}

void Light::enable()
{

	matrix.transformVec4f(position);	// Transform our array of 4 floats by this matrix
	for(unsigned int i =0; i< programs.size(); i++){
		int lightposUniformHandle = programs.at(i).getUniformHandle("light_pos");
		glUniform4fv(lightposUniformHandle, 1, position);
	}

}


void Light::setPosition(float x, float y, float z, float w){

	position[0] = x;
	position[1] = y;
	position[2] = z;
	position[3] = w;
}







